package leetCode.sample;

/**
 * @author: zhangzhiping
 * @date: 2019/11/29 16:19
 * @description: 判断数字是否为回文数
 */
public class PalindromeSolution {

    public static void main(String[] args) {

        System.out.println(isPalindrome2(1221));
        System.out.println(isPalindrome2(121));
        System.out.println(isPalindrome2(1231));
        System.out.println(isPalindrome2(-121));

    }

    private static boolean isPalindrome2(int x) {
        if (x < 0) {
            return false;
        }
        int y = x;
        int reverse = 0;
        while (y > 0) {
            reverse = reverse * 10;
            reverse = reverse + y % 10;
            y = y / 10;
        }
        return reverse == x;
    }

    private static boolean isPalindrome(int x) {
        if (x < 0) {
            return false;
        }
        String num = String.valueOf(x);
        for (int i = 0; i < num.length() / 2; i++) {
            if (num.charAt(i) == num.charAt(num.length() - i - 1)) {
                continue;
            }
            return false;
        }
        return true;
    }
}
